$(function () {

    var nameinput = $('.Name input');
    var ageinput = $('.Age input');
    var idinput = $('.idcode input');
    var newusrinput = $('.newusr input');
    var newpwdinput = $('.newpwd input');
    var repwdinput = $('.repwd input');
    var emailinput = $('.email input');
    var recodeinput = $('.recode input');

    var nameerror = $('.Name .reerror');
    var ageerror = $('.Age .reerror');
    var iderror = $('.idcode .reerror');
    var newusrerror = $('.newusr .reerror');
    var newpwderror = $('.newpwd .reerror');
    var repwderror = $('.repwd .reerror');
    var emailerror = $('.email .reerror');
    var recodeerror = $('.recode .reerror');

    // 验证码切换事件+判断事件
    function rechangeCode() {
        $.ajax({
            type: "get",
            url: "drawimg",
            async: false,
            dateType: "png",
            success: function (date) {
                $('.register .recode img').attr('src', 'data:image/png;base64,' + date);
            }
        });
    }
    $('.register .recode img').click(function () {
        rechangeCode();
    });

    // 跳出注册框
    $('.login .remember #register').click(function () {
        $('.register').css('display', 'block');
        rechangeCode();
    });

    $('.register .resubmit #back').click(function () {
        $('.register').css('display', 'none');
    });

    // 注册用户选择
    $('.register select').click(function () {
        if ($(this).val() == 1) {
            $('.register .idcode input').prop('placeholder', '授权id...');
            $('.register .newpwd input').attr('disabled', 'disabled');
            $('.register .repwd input').attr('disabled', 'disabled');
        }
        if ($(this).val() == 0) {
            $('.register .idcode input').prop('placeholder', '学号/工号...');
            $('.register .newpwd input').removeAttr('disabled');
            $('.register .repwd input').removeAttr('disabled');
        }
    })

    // 聚焦事件
    nameinput.focus(function () {
        nameinput.css('border-color', '#1890ff');
    });
    nameinput.blur(function () {
        nameinput.css('border-color', '#dcdfe6');
    });

    ageinput.focus(function () {
        ageinput.css('border-color', '#1890ff');
    });
    ageinput.blur(function () {
        ageinput.css('border-color', '#dcdfe6');
    });

    idinput.focus(function () {
        idinput.css('border-color', '#1890ff');
    });
    idinput.blur(function () {
        idinput.css('border-color', '#dcdfe6');
    });

    newusrinput.focus(function () {
        newusrinput.css('border-color', '#1890ff');
    });
    newusrinput.blur(function () {
        newusrinput.css('border-color', '#dcdfe6');
    });

    newpwdinput.focus(function () {
        newpwdinput.css('border-color', '#1890ff');
    });
    newpwdinput.blur(function () {
        newpwdinput.css('border-color', '#dcdfe6');
    });

    repwdinput.focus(function () {
        repwdinput.css('border-color', '#1890ff');
    });
    repwdinput.blur(function () {
        repwdinput.css('border-color', '#dcdfe6');
    });

    emailinput.focus(function () {
        emailinput.css('border-color', '#1890ff');
    });
    emailinput.blur(function () {
        emailinput.css('border-color', '#dcdfe6');
    });

    recodeinput.focus(function () {
        recodeinput.css('border-color', '#1890ff');
    });
    recodeinput.blur(function () {
        recodeinput.css('border-color', '#dcdfe6');
    });

    //性别选定变颜色
    // 记住我事件
    $('.register .Sex input').click(function () {
        if ($('.register .Sex .man input').is(':checked')) {
            $('.register .Sex .man label').css('color', '#1890ff');
        }
        if ($('.register .Sex .man input').is(':not(:checked)')) {
            $('.register .Sex .man label').css('color', '#606266');
        }
        if ($('.register .Sex .woman input').is(':checked')) {
            $('.register .Sex .woman label').css('color', '#1890ff');
        }
        if ($('.register .Sex .woman input').is(':not(:checked)')) {
            $('.register .Sex .woman label').css('color', '#606266');
        }
    });
    //按钮事件+判断
    $('.register .resubmit #resubmit').click(function () {
        nameerror.html('');
        ageerror.html('');
        iderror.html('');
        newusrerror.html('');
        newpwderror.html('');
        repwderror.html('');
        emailerror.html('');
        recodeerror.html('');
        var flag = true;
        if (nameinput.val() === null || nameinput.val() === "") {
            nameerror.html('姓名为空！！！');
            nameinput.css('border-color', '#ff4949');
            flag = false;
        }

        if (ageinput.val() == null || ageinput.val() == "") {
            ageerror.html('年龄为空！！！');
            ageinput.css('border-color', '#ff4949');
            flag = false;
        }

        if (idinput.val() == null || idinput.val() == "") {
            if ($('.register select').val() == 0) {
                iderror.html('学号/工号为空！！！');
            }
            if ($('.register select').val() == 1) {
                iderror.html('授权id为空...');
            }
            idinput.css('border-color', '#ff4949');
            flag = false;
        }

        if (newusrinput.val() == null || newusrinput.val() == "") {
            newusrerror.html('用户名为空！！！');
            newusrinput.css('border-color', '#ff4949');
            flag = false;
        }

        if (newpwdinput.val() == null || newpwdinput.val() == "") {
            if ($('.register select').val() == 0) {
                newpwderror.html('密码为空！！！');
                newpwdinput.css('border-color', '#ff4949');
                flag = false;
            }
            if ($('.register select').val() == 1) {
                newpwdinput.css('border-color', '#dcdfe6');
            }
        }

        if (repwdinput.val() == null || repwdinput.val() == "") {
            if ($('.register select').val() == 0) {
                repwderror.html('重复密码为空！！！');
                repwdinput.css('border-color', '#ff4949');
                flag = false;
            }
            if ($('.register select').val() == 1) {
                repwdinput.css('border-color', '#dcdfe6');
            }
        }

        if (emailinput.val() == null || emailinput.val() == "") {
            emailerror.html('邮箱为空！！！');
            emailinput.css('border-color', '#ff4949');
            flag = false;
        }

        if (recodeinput.val() == null || recodeinput.val() == "") {
            recodeerror.html('验证码为空！！！');
            recodeinput.css('border-color', '#ff4949');
            flag = false;
        }

        if (newpwdinput.val() != repwdinput.val()) {
            if ($('.register select').val() == 0) {
                repwderror.html('重复密码不一致！！！');
                repwdinput.css('border-color', '#ff4949');
                flag = false;
            }
            if ($('.register select').val() == 1) {
                newpwdinput.css('border-color', '#dcdfe6');
            }
        }
        if (flag) {
            $.ajax({
                type: "post",
                url: "userregister",
                data: {
                    "select": $('.register select').val(),
                    "name": nameinput.val(),
                    "sex": $('.register .Sex input:checked').val(),
                    "age": ageinput.val(),
                    "user_id": idinput.val(),
                    "username": newusrinput.val(),
                    "password": repwdinput.val(),
                    "email": emailinput.val(),
                    "code": recodeinput.val(),
                },
                success: function (date) {
                    if (date == 1) {
                        newusrerror.html('用户名存在！！！');
                        newusrinput.css('border-color', '#ff4949');
                    } else if (date == 2) {
                        if($('.register select').val()==0){
                            iderror.html('学号/工号存在！！！');
                        }
                        if($('.register select').val()==1){
                            iderror.html('授权id已使用/不存在！！！');
                        }
                        idinput.css('border-color', '#ff4949');
                    } else if (date == 3) {
                        $('.register .hidden').css('display', 'block');
                        $('.register .hidden .add_success').css('display', 'block');
                    } else if (date == 4) {
                        $('.register .hidden').css('display', 'block');
                        $('.register .hidden .add_error').css('display', 'block');
                    } else if (date == 5) {
                        recodeerror.html('验证码错误！！！');
                        recodeinput.css('border-color', '#ff4949');
                    }
                }
            });
        }
    });
    // 隐藏域按钮事件
    // 成功
    $('.register .hidden .add_success input').click(function () {
        $('.register .hidden').css('display', 'none');
        $('.register .hidden .add_success').css('display', 'none');
        // 刷新验证码
        rechangeCode();
    });
    // 失败
    $('.register .hidden .add_error input').click(function () {
        $('.register .hidden').css('display', 'none');
        $('.register .hidden .add_error').css('display', 'none');
        rechangeCode();
    });
});